local niye = fk.CreateSkill{
  name = "investiture__niye",
  tags = {"Turn"}
}

Fk:loadTranslationTable{
  ["investiture__niye"] = "溺业",
  [":investiture__niye"] = "回合技，当你摸牌时，可改为摸五张牌并明置。你使用红色明置牌仅能以两张牌转化【万箭齐发】的方式使用；"
  .."你的回合内，若你杀死过角色，你的黑色明置牌视为【酒】，且濒死角色可以将你的一张黑色明置牌当【酒】②使用。",

  ["#investiture__niye-invoke"] = "溺业：你将摸%arg张牌，可以改为摸五张牌并明置",
  ["#investiture__niye-use"] = "溺业：可以将 %dest 的一张黑色明置牌当【酒】使用",
  ["@@investiture__niye_killed"] = "溺业",

  ["$investiture__niye1"] = "自汤武以来，百方无不靡伏，诸公何惧之有？",
  ["$investiture__niye2"] = "不过攀叶之虫，岂伤奔泥大象？",
}

local DIY = require "packages/diy_utility/diy_utility"
local TuntianUtil = require "packages/tuntian_studio/utility"

niye:addAcquireEffect(function (self, player, is_start)
  if is_start then return end
  if #player.room.logic:getEventsOfScope(GameEvent.Death, 1, function (e)
    return e.data.damage and e.data.damage.from and e.data.damage.from == player
  end, Player.HistoryGame) == 1 then
    player.room:setPlayerMark(player, "@@investiture__niye_killed", 1)
  end
end)

niye:addEffect(fk.BeforeCardsMove, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill("investiture__niye") and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 then
      for _, move in ipairs(data) do
        if move.to == player and move.toArea == Card.PlayerHand and move.moveReason == fk.ReasonDraw then
          event:setCostData(self, move)
          return true
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local move = event:getCostData(self) ---@type MoveCardsData
    return player.room:askToSkillInvoke(player, { skill_name = "investiture__niye", prompt = "#investiture__niye-invoke:::"..#move.moveInfo })
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Card.PlayerHand and move.moveReason == fk.ReasonDraw then
        move.moveInfo = {}
      end
    end

    DIY.showCards(player, room:drawCards(player, 5, niye.name))
  end
})

niye:addEffect("prohibit", {
  prohibit_use = function (self, player, card)
    if player:hasSkill("investiture__niye") and table.contains(player:getCardIds("h"), card.id)
    and TuntianUtil.isShown(card.id) and card.color == Card.Red then
      return true
    end
  end
})

niye:addEffect("viewas", {
  anim_type = "offensive",
  pattern = "archery_attack",
  card_filter = function (self, player, to_select, selected)
    return #selected < 2 and TuntianUtil.isShown(to_select)
    and Fk:getCardById(to_select).color == Card.Red
  end,
  view_as = function (self, player, cards)
    if #cards ~= 2 then return end
    local card = Fk:cloneCard("archery_attack")
    card.skillName = niye.name
    card:addSubcards(cards)
    return card
  end,
})

niye:addEffect(fk.Death, {
  can_refresh = function (self, event, target, player, data)
    return data.damage and data.damage.from and data.damage.from == player
    and player:hasSkill(niye.name, true)
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "@@investiture__niye_killed", 1)
  end
})

niye:addEffect("filter", {
  anim_type = "special",
  card_filter = function(self, card, player)
    return player:hasSkill(niye.name) and player:getMark("@@investiture__niye_killed") == 1
    and player == Fk:currentRoom().current and TuntianUtil.isShown(card.id)
    and table.contains(player:getCardIds("h"), card.id) and card.color == Card.Black
  end,
  view_as = function(self, player, to_select)
    return Fk:cloneCard("analeptic", to_select.suit, to_select.number)
  end,
})

niye:addEffect(fk.EnterDying, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(niye.name) and not target:prohibitUse(Fk:cloneCard("analeptic"))
    and player:getMark("@@investiture__niye_killed") == 1 and player == player.room.current
    and table.find(player:getCardIds("h"), function (c)
      return TuntianUtil.isShown(c) and Fk:getCardById(c).color == Card.Black
    end)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local ids = table.filter(player:getCardIds("h"), function (c)
      return Fk:getCardById(c).color == Card.Black and TuntianUtil.isShown(c)
    end)

    local card = room:askToCards(target, {
      skill_name = niye.name,
      min_num = 1,
      max_num = 1,
      pattern = tostring(Exppattern{ id = ids }),
      prompt = "#investiture__niye-use::"..player.id,
      cancelable = true,
      expand_pile = ids,
    })
    if #card > 0 then
      event:setCostData(self, card[1])
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local id = event:getCostData(self) ---@type integer
    local card = Fk:cloneCard("analeptic")
    card.skillName = niye.name
    card:addSubcard(id)
    room:useCard{
      from = target,
      tos = { target },
      extra_data = {
        analepticRecover = true,
      },
      card = card,
    }
  end,
})

return niye